using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class Sprinting201 : SkillBasic
{
    public GameObject sprintgingtimes;
    public Transform playertransform;
    public PlayerBasic player;
    public PlayerAttack playerattack;
    private Rigidbody2D myRigidbody;
    public float damageratio;
    private TrailRenderer trail;
    public float skilltime;
    public int face;
    public float runspeedratio;
    private bool IsSkilling;
    public CameraFollow camerafollow;
    float gs;
    private BoxCollider2D collider;

    public float StiffTime;
    public float RepelDistance;
    public int RepelTimes;
    public float CD;
    private float CDtime;
    public bool skillend;
    // Start is called before the first frame update
    public TextMeshProUGUI text;
    int skilltimes;
    public void Start()
    {
        playertransform = GetComponentInParent<Transform>();
        playerattack = GameObject.FindWithTag("Attack").GetComponent<PlayerAttack>();
        camerafollow = GameObject.Find("CameraFollow").GetComponent<CameraFollow>();
        myRigidbody = GetComponentInParent<Rigidbody2D>();
        trail = GetComponentInChildren<TrailRenderer>();
        player = GetComponentInParent<PlayerBasic>();
        collider = GetComponentInParent<BoxCollider2D>();
        trail.enabled = false;
        IsSkilling = false;
        collider.enabled = false;
        gs = myRigidbody.gravityScale;
        SprintingCD.CD = CD;
        SprintingCD.CDCurrent = 0;
        skillend = false;
        Instantiate(sprintgingtimes, GameObject.Find("sprinting(Clone)").transform);
        text=GameObject.Find("springtingtimes(Clone)").GetComponent<TextMeshProUGUI>();
        skilltimes = 5;
    }

    // Update is called once per frame
    public IEnumerator Skilling()
    {
        yield return new WaitForSeconds(skilltime);
        if(IsSkilling)skillingend();

    }
    public void Skill()
    {
        camerafollow.smoothing = 0.1f;
        player.attackable--;
        player.movable = false;
        trail.enabled = true;
        IsSkilling = true;
        collider.enabled = true;
        StartCoroutine(Skilling());

    }
    void skillingend()
    {
        player.attackable++;
        player.movable = true;
        trail.enabled = false;
        myRigidbody.velocity = new Vector2(0, myRigidbody.velocity.y);
        IsSkilling = false;
        collider.enabled = false;
        myRigidbody.gravityScale = gs;
        Time.timeScale = 1f;
        camerafollow.smoothing = 1f;
    }
    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Enemy"))
        {

            other.GetComponent<EnemyBasic>().TakeDamage(damageratio * playerattack.damage, face, RepelTimes, RepelDistance, StiffTime);
        }
        if (other.gameObject.CompareTag("GrindingTower"))
        {
            other.GetComponent<Grinding>().TakeDamage(damageratio * playerattack.damage);
        }
        if (other.gameObject.CompareTag("Air"))
        {
            myRigidbody.velocity = Vector2.zero;
            if (IsSkilling) skillingend();
        }
    }
    public void Update()
    {
        face = player.face;
        if (CDtime > 0)
        {
            CDtime -= Time.deltaTime;
            SprintingCD.CDCurrent = CDtime;
        }
        else
        {
            if (skilltimes < 3)
            {
                skilltimes++;
                if(skilltimes<3)
                    CDtime = CD;
                
            }
        }
        text.text =skilltimes.ToString();
        if (Input.GetKeyDown(KeyCode) && Time.timeScale > 0)
        {
            if (skilltimes>0&&(!IsSkilling))
            {
                skilltimes--;
                Skill();
                if (skilltimes == 2)
                {
                    CDtime = CD;
                }
            }


        }
        if (IsSkilling)
        {
            Time.timeScale = 0.8f;
            myRigidbody.velocity = new Vector2(-face * runspeedratio * player.Runspeed, 0);
            myRigidbody.gravityScale = 0;
        }
    }
}
